﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IniFiles;

namespace Cali
{
    public partial class Form_FilePicker : Form
    {
        public Form_FilePicker()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "ini(*.ini)|*.ini";
            ofd.InitialDirectory = Application.StartupPath;

            if (ofd.ShowDialog() == DialogResult.OK)
                txb_ini.Text = ofd.FileName;
        }

        //protected override void WndProc(ref Message m)
        //{
        //    const int WM_SYSCOMMAND = 0x0112;
        //    const int SC_CLOSE = 0xF060;
        //    if((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_CLOSE))
        //    {
        //        if (txb_ini.Text.Contains("General.ini"))
        //        {
        //            MessageBox.Show("不能选择General.ini，请重新选择。", "温馨提示", MessageBoxButtons.OK);
        //            return;
        //        }
        //        if (txb_ini.Text.Length == 0)
        //        {
        //            MessageBox.Show("未选择配置文件，请选择。", "温馨提示", MessageBoxButtons.OK);
        //            return;
        //        }
        //    }
        //    base.WndProc(ref m);
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(Form_Main.m_PathGeneral, true, System.Text.Encoding.Default))
                sw.Close();

            string path = txb_ini.Text;
            if (path.EndsWith(".ini"))
            {
                if (path.Contains("General.ini"))
                {
                    MessageBox.Show("不能选择General.ini，请重新选择。", "温馨提示", MessageBoxButtons.OK);
                    return;
                }
                if(path.Contains("Ins.ini"))
                {
                    MessageBox.Show("不能选择Ins.ini，请重新选择。", "温馨提示", MessageBoxButtons.OK);
                    return;
                }
                if (File.Exists(path))
                {
                    IniFile file = new IniFile(Form_Main.m_PathGeneral);
                    file.WriteString("Settings", "iniPath", path);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("选择的文件不存在，请重新选择。");
                    return;
                }
            }
            else
            {
                if(MessageBox.Show("ini配置档选择错误，确定要退出？", "温馨提示", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
                {
                    this.Close();
                }
                return;
            }
        }

        private void Form_FilePicker_Load(object sender, EventArgs e)
        {
            string path = Path.Combine(Application.StartupPath, "General.ini");
            if(File.Exists(path))
            {
                IniFile file = new IniFile(path);
                txb_ini.Text = file.ReadString("Settings", "iniPath", "");
            }
        }
    }
}
